<?php
session_start ();
// 判断用户是否已经登录
if (! isset ( $_SESSION ["userinfo"] ) and empty ( $_SESSION ["userinfo"] ["userid"] )) {
	session_unset ();
	session_destroy ();
	header ( "location:../index.php" );
	exit ();
}
require_once './AdminService.class.php';
require_once '../tools/SqlHelper.class.php';
// 页面中默认需要的字段
$Infoid = "";
$Infoname = "";
$Infopassword = "";
$Infosex = "";
$birthday = "";
// 从列表中传递过来的图书标识，进入修改状态，如果不存在则为0，进入新建状态
$_id = 0;
/**
 * 修改信息
 */
if (isset ( $_GET ['_id'] )) {
	$_id = ( int ) $_GET ['_id'];
	$sqlHelper = new SqlHelper ();
	$sql = "select * from user_tb WHERE `user_tb_id`=$_id";
	$res = $sqlHelper->execute_dql ( $sql );
	$row = mysql_fetch_array ( $res );
	if ($row) {
		$Infoid = $row ['user_id'];
		$Infoname = $row ['user_name'];
		$Infopassword = $row ['user_password'];
	}
	$sql = "select * from personal_data_tb WHERE `user_id`='$Infoid'";
	$res = $sqlHelper->execute_dql ( $sql );
	$row = mysql_fetch_array ( $res );
	if ($row) {
		$Infosex = $row ['ps_sex'];
		$birthday = $row ['ps_birthday_year'];
	}
	if ($Infosex == "0") {
		$Infosex = "男";
	} else {
		$Infosex = "女";
	}
	$now_data = getdate ();
	$birthday = ($now_data ["year"] - ( int ) $birthday);
	mysql_free_result ( $res );
	// 关闭连接
	$sqlHelper->close_connect ();
} else if (isset ( $_POST ['user_submit'] ) and $_POST ['user_submit'] == "确认添加" || $_POST ['user_submit'] == "确认修改") {
	// 获取浏览器发过来的页面数据
	$bid = ( int ) $_POST ['bid'];
	$Info_id = $_POST ['Info_id'];
	$Info_name = $_POST ['Info_name'];
	$Info_password = $_POST ['Info_password'];
	$Info_sex = $_POST ['Info_sex'];
	$birthday = $_POST ['birthday'];
	if ((strlen ( $Info_id ) < 6) || (strlen ( $Info_name ) < 2) || (strlen ( $Info_password ) < 6) || ($Info_sex == "")) {
		echo "<script>alert('有未填写项或填写格式不正确')</script>";
	} else {
		// 实例化一个AdminServive方法,判断用户是否被注册
		$adminService = new AdminService ();
		if ($bid == 0) {
			$name = $adminService->chekcIsHaveAdimn ( $Info_id );
			if (! $name) {
				echo "<script>alert('已存在该用户')</script>";
			} else {
				$isAdd = $adminService->addAdimn ( $Info_id, $Info_password, $Info_name, $Info_sex, $birthday );
				if (! ! $isAdd) {
					if ($bid != 0) {
						header ( "location:manage.php" );
					} else {
						echo "<script>alert('添加人员成功')</script>";
					}
				} else {
					echo "<script>alert('该会员已经存在')</script>";
				}
			}
		} else {
			$isAdd = $adminService->chageAdimn ( $bid, $Info_id, $Info_password, $Info_name, $Info_sex, $birthday );
			if (! ! $isAdd) {
				header ( "location:infoList.php" );
			} else {
				echo "<script>alert('修改会员信息失败')</script>";
			}
		}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="./style/register.css" />
<title>添加会员</title>
</head>
<body>

	<form action="./afterInfo.php" target="frm" method="post">
		<input type="hidden" name="bid" value="<?php echo $_id;?>" />
		<div>
			<h1>添加会员</h1>
			<h2>会员信息</h2>
			<div class="table_style">
				<label> 用户名： </label> <input type="text" name=Info_id
					value="<?php echo $Infoid;?>"> </input>
			</div>
			<div class="table_style">
				<label> 姓名： </label> <input type="text" name=Info_name
					value="<?php echo $Infoname;?>"> </input>
			</div>
			<div class="table_style">
				<label> 密码： </label> <input type="password" name="Info_password"
					value=""> </input>
			</div>
			<div class="table_style">
				<label> 性别： </label> <input type="text" name="Info_sex"
					value="<?php echo $Infosex;?>"> </input>
			</div>
			<div class="table_style">
				<label> 年龄： </label> <input type="text" name="birthday"
					value="<?php echo $birthday;?>"> </input>
			</div>
			<h2></h2>
			<div class="button_style">
				<input class="button" name="user_submit" type="submit"
					value=<?php
					if ($_id != 0) {
						echo "确认修改";
					} else {
						echo "确认添加";
					}
					?> /> 
				 <?php
					if ($_id == 0) {
						echo "<input class='button' type='reset' value='重新填写' />";
					}
					?>
			</div>
		</div>
	</form>
</body>
</html>
